#include "./test.h"
#include "../include/pool.h"

TEST(POOL)
{
    Pool pool;
    Pool_Init(&pool, (u_short[][2]){
                         {8, 4},
                         {16, 32},
                         {32, 32},
                         {48, 32},
                         {64, 32},
                         {512, 2},
                         {0, 0},
                     });
    assert(pool.mblock_length == 6);

    void *p1 = Pool_Malloc(&pool, 32);
    void *p2 = Pool_Malloc(&pool, 33);
    void *p3 = Pool_Malloc(&pool, 34);

    assert(pool.mblock_count[2] == 1);
    assert(pool.mblock_count[3] == 1);

    MBlock *block1 = POINTER_TO_MBLOCK(pool.mblock_info, p1);
    assert(block1->usedCount == 1);
    assert(block1->index == 2);
    assert(block1->previous == NULL && block1->next == NULL);

    MBlock *block2 = POINTER_TO_MBLOCK(pool.mblock_info, p2);
    assert(block2->usedCount == 2);
    assert(block2->index == 3);
    assert(block2->previous == NULL && block2->next == NULL);

    Pool_Free(&pool, p1);
    Pool_Free(&pool, p2);
    Pool_Free(&pool, p3);
    assert(pool.mblock_count[2] == 0);
    assert(pool.mblock_count[3] == 0);

    Delete_Pool(&pool);
}